﻿using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Office.OneNote.Interop;

namespace Microsoft.Office.OneNote.XmlViewer.Controls.Views
{
    public partial class ViewXml : ViewBase
    {
        private string _tempPath;

        public ViewXml()
        {
            InitializeComponent();
            browser.StatusTextChanged += new EventHandler(browser_StatusTextChanged);
        }

        public override void LeaveCurrentObject()
        {
            browser.Stop();

            do
            {
                Application.DoEvents();
            }
            while (browser.IsBusy);

            DeleteTempFile();
        }

        public override void Navigate(OneNoteHierarchyObject obj)
        {
            if (obj == null)
            {
                browser.Navigate("about:blank");
                text.Text = string.Empty;
            }
            else
            {
                StatusText = "Retrieving object " + obj.ObjectId + " from OneNote...";
                ParentForm.Cursor = Cursors.WaitCursor;
                Application.DoEvents();

                Navigate(GetXml(obj));
            }
        }

        public override void Navigate(OneNoteHierarchy hierarchy)
        {
            Navigate(OneNoteApplication.Current.GetHierarchy(OneNoteObjectId.Empty, HierarchyScope.Pages));
        }

        public override void Navigate(XDocument doc)
        {
            _tempPath = CreateTempFile(doc);
            browser.Navigate(_tempPath);
            text.Text = doc.ToString();
        }

        private void browser_StatusTextChanged(object sender, EventArgs e)
        {
            string str = browser.StatusText;

            if (string.IsNullOrEmpty(str))
            {
                str = "Done";
            }

            StatusText = str;
        }

        private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            DeleteTempFile();
            ParentForm.Cursor = Cursors.Default;
        }

        private void DeleteTempFile()
        {
            if (_tempPath != null)
            {
                File.Delete(_tempPath);
                _tempPath = null;
            }

        }

        private XDocument GetXml(OneNoteObjectWithIdentity obj)
        {
            OneNotePage page = (obj as OneNotePage);

            if (page != null)
            {
                return OneNoteApplication.Current.GetPageContent(page.ObjectId);
            }
            else
            {
                return OneNoteApplication.Current.GetHierarchy(obj.ObjectId, HierarchyScope.Children);
            }
        }

        private string CreateTempFile(XDocument xml)
        {
            string tempPath = GetTempPath();

            using (XmlWriter writer = new XmlTextWriter(tempPath, Encoding.UTF8))
            {
                xml.WriteTo(writer);
            }

            return tempPath;
        }

        private string GetTempPath()
        {
            return Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Path.GetRandomFileName(), ".xml"));
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            browser.Visible = checkBox1.Checked;
            text.Visible = !checkBox1.Checked;
        }
    }
}
